package com.hos.check.ds;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.admin.SnapshotIF;


public class DBPoolManager {
	private final static Log log = LogFactory.getLog(DBPoolManager.class);

	public static void registerPool(Map<String, String> map) throws Exception {
		Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
		Properties info = new Properties();
		info.setProperty("proxool.minimum-connection-count", getValue(map
				.get("minCount"), "0"));
		info.setProperty("proxool.maximum-connection-count", getValue(map
				.get("maxCount"), "50"));
		info.setProperty("proxool.maximum-active-time", getValue(map
				.get("maxActiveTime"), "300000"));
		info.setProperty("proxool.maximum-connection-lifetime", getValue(map
				.get("maxConnLifeTime"), "3600000"));
		// info.setProperty("proxool.house-keeping-test-sql", "select
		// CURRENT_DATE");
		info.setProperty("user", getValue(map.get("user"), ""));
		info.setProperty("password", getValue(map.get("password"), ""));
		String alias = getValue(map.get("alias"), "");
		String driverClass = getValue(map.get("driver"), "");
		String driverUrl = getValue(map.get("url"), "");
		String url = "proxool." + alias + ":" + driverClass + ":" + driverUrl;
		ProxoolFacade.registerConnectionPool(url, info);
	}

	/**
	 * 此方法可以得到连接池的信息 showSnapshotInfo
	 */
	private static void showSnapshotInfo(String name) {
		try {
			SnapshotIF snapshot = ProxoolFacade.getSnapshot(name, true);
			int curActiveCount = snapshot.getActiveConnectionCount();// 获得活动连接数
			int availableCount = snapshot.getAvailableConnectionCount();// 获得可得到的连接数
			int maxCount = snapshot.getMaximumConnectionCount();// 获得总连接数
			if (curActiveCount != availableCount)// 当活动连接数变化时输出的信息
			{
				if(log.isDebugEnabled()){
					log.debug("活动连接数:" + curActiveCount
						+ "(active)  可得到的连接数:" + availableCount
						+ "(available)  总连接数:" + maxCount + " (max)");
				}
				availableCount = curActiveCount;
			}
		} catch (ProxoolException e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection(String alias) throws Exception {
		if(log.isDebugEnabled()){
			showSnapshotInfo(alias);
		}
		return DriverManager.getConnection("proxool." + alias);
	}

	public static boolean isInDBPool(String alias) {
		String[] aliases = ProxoolFacade.getAliases();
		if (aliases == null) {
			return false;
		}
		for (int i = 0; i < aliases.length; i++) {
			if (aliases[i].equals(alias)) {
				return true;
			}
		}
		return false;
	}

	private static String getValue(String value, String defValue) {
		if (StringUtils.isNotEmpty(value)) {
			return value;
		} else {
			return defValue;
		}
	}
}